﻿#include "positionsettingwid.h"
#include <QMessageBox.h>
#include "ui_positionsettingwid.h"
#include "../component/componentmanager.h"

PositionSettingWid::PositionSettingWid(QWidget *parent)
	: QWidget(parent)
	, ui(new Ui::PositionSettingWid())
{
	ui->setupUi(this);
	setStyleSheet("font: 12pt \"Agency FB\"");
	m_rob = component->GetComponent(Component::component_robot);
}

PositionSettingWid::~PositionSettingWid()
{
	delete ui;
}

void PositionSettingWid::on_robotGet_clicked()
{
	if (m_rob == nullptr) {
		if (!component->InitForSystemSetting()) {
			QMessageBox::warning(this, "ERROR", "机械手组件初始化失败，无法获取坐标");
			return;
		}
		m_rob = component->GetComponent(Component::component_robot);
		if (m_rob == nullptr) {
			QMessageBox::warning(this, "ERROR", "机械手组件无法获取，无法获取坐标");
			return;
		}
	}
	QByteArray output;
	bool ret = m_rob->ExcuteCmd((int)RobotCmdType::robot_cmd_get_target, {}, output);
	if (!ret) {
		QMessageBox::warning(this, "ERROR", "机械手坐标读取失败");
		return;
	}
	PositionJ pos = {};
	pos.fromJson(output);
	Init(pos);
}

void PositionSettingWid::Init(const PositionJ& pos)
{
	ui->x->setValue(pos.x());
	ui->y->setValue(pos.y());
	ui->z->setValue(pos.z());
	ui->a->setValue(pos.a());
	ui->b->setValue(pos.b());
	ui->c->setValue(pos.c());
}
PositionJ PositionSettingWid::GetPositon()
{
	PositionJ out = {};
	out.x = ui->x->value();
	out.y = ui->y->value();
	out.z = ui->z->value();
	out.a = ui->a->value();
	out.b = ui->b->value();
	out.c = ui->c->value();
	return out;
}
